/***************************************************************************
*   Copyright (C) 2010-2011 by swkyer <swkyer@gmail.com>                  *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#ifndef __EJTAG_H__
#define __EJTAG_H__

#include "../JlinkUSB/JlinkUSB.h"
#include "../JlinkUSB/JlinkProtocol.h"
#include "JlinkMIPSExport.h"

typedef uint32_t ubase_t;


/* TAP Controller State Transitions */
typedef enum tap_state
{
	TAP_INVALID = -1,
	TAP_DREXIT2 = 0x0,
	TAP_DREXIT1 = 0x1,
	TAP_DRSHIFT = 0x2,
	TAP_DRPAUSE = 0x3,
	TAP_IRSELECT = 0x4,
	TAP_DRUPDATE = 0x5,
	TAP_DRCAPTURE = 0x6,
	TAP_DRSELECT = 0x7,
	TAP_IREXIT2 = 0x8,
	TAP_IREXIT1 = 0x9,
	TAP_IRSHIFT = 0xa,
	TAP_IRPAUSE = 0xb,
	TAP_IDLE = 0xc,
	TAP_IRUPDATE = 0xd,
	TAP_IRCAPTURE = 0xe,
	TAP_RESET = 0x0f,
} tap_state_t;

#define MAX_CORES						8

#define EJTAG_TRST_PULSE_MS				200
#define EJTAG_RST_PULSE_MS				500

#define EJTAG_INSTRUCTION_LENGTH		5
#define EJTAG_REGISTER_LENGTH			32
#define EJTAG_FASTDATA_LENGTH			1
#define EJTAG_BYPASS_LENGTH				1

#define JTAG_REGISTER_EXTEST			0x00
#define JTAG_REGISTER_IDCODE			0x01
#define JTAG_REGISTER_SAMPLE			0x02
#define JTAG_REGISTER_IMPCODE			0x03
#define JTAG_REGISTER_ADDRESS			0x08
#define JTAG_REGISTER_DATA				0x09
#define JTAG_REGISTER_CONTROL			0x0A
#define JTAG_REGISTER_ALL				0x0B
#define JTAG_REGISTER_EJTAG_BOOT		0x0C
#define JTAG_REGISTER_NORMAL_BOOT		0x0D
#define JTAG_REGISTER_EJWATCH			0x1C
#define JTAG_REGISTER_BYPASS			0x1F


#define EJTAG_MAX_BIT_STREAM_LENGTH		(1024*sizeof(uint8_t))

typedef struct __ejtag
{
	tap_state_t state;
	int ir_len;
	int dr_len;

	int trst_time;
	int rst_time;

	int jlinkdev;
	jlink_usb_t *usbdev;

	jlink_caps_t caps;
	jlink_speed_t speed;
	jlink_state_t pinstate;
	jlink_hwinfo_t hwinfo;
	jlink_jtag_acc_t jacc;
	jlink_unknow_09_t u09;
} ejtag_t;


JLINKMIPS_API ejtag_t *ejtag_init(int jlinkdev);
JLINKMIPS_API void ejtag_exit(ejtag_t *pejtag);
JLINKMIPS_API ejtag_t *ejtag_reinit(ejtag_t *pejtag);

JLINKMIPS_API int ejtag_is_target_connect(ejtag_t *pejtag);

JLINKMIPS_API int ejtag_trst_activate(ejtag_t *pejtag);
JLINKMIPS_API int ejtag_trst_deactivate(ejtag_t *pejtag);
JLINKMIPS_API int ejtag_rst_activate(ejtag_t *pejtag);
JLINKMIPS_API int ejtag_rst_deactivate(ejtag_t *pejtag);

JLINKMIPS_API int ejtag_tap_reset(ejtag_t *pejtag, int use_trst);
JLINKMIPS_API int ejtag_runtest_idle(ejtag_t *pejtag);

JLINKMIPS_API int ejtag_data(ejtag_t *pejtag, ubase_t data_out, ubase_t *pdatain);
JLINKMIPS_API int ejtag_instruction(ejtag_t *pejtag, ubase_t ir_out, ubase_t *pirin);

JLINKMIPS_API int ejtag_get_idcode(ejtag_t *pejtag, uint32_t *idcode);
JLINKMIPS_API int ejtag_get_impcode(ejtag_t *pejtag, uint32_t *impcode);



#endif /* end of __EJTAG_H__ */
